package screens;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import core.UIController;

public class LoginForm extends Form implements CommandListener {

	private static LoginForm instance;

	private TextField username;
	private TextField password;

	private Command commandLogin;
	private Command commandSair;

	public static LoginForm getInstance() {
		if (instance == null) {
			instance = new LoginForm();
		}
		return instance;
	}

	private LoginForm() {
		super("Login");

		this.username = new TextField("login:", "", 10, TextField.ANY);
		this.password = new TextField("senha:", "", 10, TextField.PASSWORD);

		this.commandLogin = new Command("Entrar", Command.OK, 1);
		this.commandSair = new Command("Sair", Command.EXIT, 1);

		append(this.username);
		append(this.password);

		addCommand(commandLogin);
		addCommand(commandSair);

		setCommandListener(this);
	}

	public void commandAction(Command command, Displayable displayable) {
		try {
			if (command.equals(commandLogin)) {
				UIController.getInstance().login(username.getString(),
						password.getString());
			} else if (command.equals(commandSair)) {
				UIController.getInstance().sair();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
